package cn.seaboot.admin.starter.web;

import cn.seaboot.admin.file.bean.AppInfo;
import cn.seaboot.admin.logger.bean.LogType;
import cn.seaboot.admin.logger.manager.ActionLogger;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.Problem;
import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.mvc.response.FastJsonView;
import cn.seaboot.admin.starter.service.DebugService;
import cn.seaboot.commons.core.ClassPathResource;
import cn.seaboot.commons.core.Converter;
import cn.seaboot.commons.core.FastJsonUtils;
import cn.seaboot.commons.exception.BizException;
import cn.seaboot.commons.file.IOUtils;
import cn.seaboot.commons.file.ReaderUtils;
import cn.seaboot.commons.file.ResponseWriter;
import cn.seaboot.commons.lang.P;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author ChenSS on 2018/6/19.
 */
@Controller
@RequestMapping("debug")
public class DebugCtrl {
    private Logger logger = LoggerFactory.getLogger(DebugCtrl.class);

    @Resource
    private DebugService service;

    @ResponseBody
    @RequestMapping("valid")
    public Result valid(@Validated AppInfo info) {
        return Result.failed();
    }

    @ResponseBody
    @RequestMapping("result")
    public Result result() {
        // do sth.
        logger.debug("1312123");
        logger.debug("1312123");
        return Result.succeed();
    }

    @RequestMapping("transactional")
    public int transactional() {
        return service.testTransactional();
    }

    @RequestMapping("download.*")
    public void result(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = "这是一张图片.png";
        try (InputStream is = IOUtils.openFileInputStream(new File("C:\\Users\\ASUS\\Desktop\\111.png"))) {
            IOUtils.copy(is, response.getOutputStream());
            ResponseWriter.preview(response, is, name, P.filenameEncode());
        }
    }

    @ResponseBody
    @RequestMapping("logger")
    @ActionLogger(type = LogType.AUTHORITY, operation = "${id}开始做功课!")
    public Object logger(@RequestBody(required = false) String body) {
        System.out.println("--------------------");
        System.out.println(body);
        return 1;
    }

    @ResponseBody
    @RequestMapping("body")
    public Object body(@RequestBody(required = false) AppInfo info) {
        System.out.println("--------------------");
        System.out.println(info);
        return 1;
    }

    @ResponseBody
    @RequestMapping("throw")
    public Problem exception() {
//        return Problem.builder().withTitle("123123").withStatus(HttpStatus.INTERNAL_SERVER_ERROR).build();
        throw new BizException("-----------------");
    }

    @ResponseBody
    @RequestMapping("params/{a}")
    public void exception(@PathVariable String a) {
        System.out.println(a);
    }

    @ResponseBody
    @RequestMapping("data")
    public int data(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        System.out.println(request.getServletPath());
        System.out.println(request.getQueryString());
        System.out.println(request.getContextPath());
        return 1;
    }

    @RequestMapping("view")
    public ModelAndView view(ModelAndView mv) {
        FastJsonView jsonView = new FastJsonView();
        jsonView.addStaticAttribute("name", "小明这是一段繁體字");
        return new ModelAndView(jsonView);
    }

    @ResponseBody
    @RequestMapping("cache")
    public Object data() {
        return service.testCache("a");
    }

    @ResponseBody
    @RequestMapping("query")
    public Object query(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map -> service.query());
    }

    @ResponseBody
    @RequestMapping("cache2")
    public Object data2() {
        return service.testCache2("a");
    }

    @ResponseBody
    @RequestMapping("/resource")
    public String resource() throws IOException {
        try (InputStream inputStream = ClassPathResource.getInputStream("data/json/ueditor/config.json")) {
            return ReaderUtils.readAsString(inputStream);
        }
    }

    @ResponseBody
    @RequestMapping("convert")
    public String convert() {
        ConversionService conversionService = Converter.getConversionService();
        try {
            Field field = GenericConversionService.class.getDeclaredField("converters");
            field.setAccessible(true);
            Object object = field.get(conversionService);
            System.out.println(object);
            System.out.println(FastJsonUtils.toJSONString(object));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(conversionService.getClass());
        System.out.println(conversionService instanceof DefaultConversionService);
        conversionService.convert("1", Integer.class);
        conversionService.convert("2020-11-24 10:10:10", java.time.LocalDateTime.class);
        return "";
    }
}
